<?php
class Bizprofile_Widgets_News_Widget extends Tomato_Widget
{
    protected function _prepareShow()
    {
        $enterprise = null;

        $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            if($user->enterprise){
                $enterprise_id = $user->enterprise->id;
            }
        }
        if(isset($params['enterprise_id'])){
            $enterprise_id = $params['enterprise_id'];
        }

        $lang = $params['lang'];

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Enterprise basic model access to db
         */
        $blogDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getBlogDao();
        $blogDao->setDbConnection($conn);
        $blogDao->setLang('vi_VN');

        /**
         * Enterprise basic model access to db
         */
        $productDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getProductDao();
        $productDao->setDbConnection($conn);
        $productDao->setLang($lang);

        /**
         * Partner access to data
        **/
        $partnerDao = Tomato_Model_Dao_Factory::getInstance()->setModule("bizprofile")->getPartnerDao();
        $partnerDao->setDbConnection($conn);
		
		 /**
         * Category models access to data
         */
        $categoryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('shop')->getCategoryDao();
        $categoryDao->setDbConnection($conn);
        $categoryDao->setLang($lang);

        /**
         * Enterprise basic model access to db
         */
        $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);

        /**
         * Get current enterprise
        **/
        $objEnterprise = $enterpriseDao->getById($enterprise_id);
        $this->_view->assign('enterprise', $objEnterprise);

        /**
        * Get latest new for enterprise
        **/
        $where = "b.enterprise_id = {$enterprise_id}";
        $partners = $partnerDao->listPartner($enterprise_id);
        $arrPartner = array();
        if(count($partners) > 0){
            foreach($partners as $partner){
                $where .= " OR b.enterprise_id = {$partner->id}";
            }
        }

        $exp = array('enterprise' => $where);
        $blogs = $blogDao->find(0, 5, $exp);

        $arrBlogs = array();
        foreach($blogs as $key=>$blog){
            /**
             * check if blog en exist
            **/
            $arrBlogs[$key] = $blog;
            if($lang == 'en_US'){
                $objBlog = $blogDao->getById($blog->blog_id, 'en_US');
                if($objBlog){
                    $arrBlogs[$key] = $objBlog;
                }
            }
        }
        $this->_view->assign('blogs', $arrBlogs);

        /**
         * Get latest product for enterprise
        **/
        $where1 = "p.enterprise_id = {$enterprise_id}";
        $exp = array('enterprise' => $where1);
        $products = $productDao->getPById($exp, 0 ,5);
        $this->_view->assign('products', $products);
		
		/**
        * Get category of product
        * Enterprise of product
        **/
        $arrPC = array();
        $arrPE = array();
        foreach($products as $product){
            $category_id = substr(substr($product->category_id,1),0,-1);
            $objCategory = $categoryDao->getById($category_id);
            $arrPC[$product->product_id] = $objCategory;

            $objEnterprise = $enterpriseDao->getById($product->enterprise_id);
            $arrPE[$product->product_id] = $objEnterprise;
        }		
        $this->_view->assign('arrPC', $arrPC);
        $this->_view->assign('arrPE', $arrPE);		
    }

    protected function _prepareConfig()
    {

    }
}
